#include "stdafx.h"
#include "LayoutVertical.h"
#include "Cell.h"
#include "FixCell.h"

LayoutVertical::LayoutVertical()
{
}

LayoutVertical::~LayoutVertical()
{
}

bool LayoutVertical::addRow(unsigned int xInHundred)
{

	m_deno += xInHundred;
	m_cells.push_back(new Cell(xInHundred));

	return true;
}

bool LayoutVertical::addFixRow(unsigned int height)
{
	m_cells.push_back(new FixCell(0, height));

	m_fixHeight += height;

	return true;
}

void LayoutVertical::updateLayout()
{
	if (m_width == 0)
	{
		return;
	}

	// find fix cell

	int nNextY = 0;
	for (std::list<Cell*>::iterator iter = m_cells.begin(); iter != m_cells.end(); ++iter)
	{
		int height = 0;
		if ((*iter)->isFixSize())
		{
			height = (*iter)->height();
		}
		else
		{
			if (m_height < m_fixHeight)
			{
				height = 0;
			}
			else
			{
				int per = (*iter)->GetxInHundred();
				if (per == 0)
				{
					SK_ASSERT("fixed cell");
				}

				height = per * (m_height - m_fixHeight) / m_deno;
			}
		}

		if (nNextY >= m_height)
		{ // fix cell size bigger then total size
			(*iter)->setY(m_height);
		}
		else
		{
			(*iter)->setY(nNextY + m_y);
		}

		(*iter)->setX(m_x);
		(*iter)->setWidth(m_width);
		(*iter)->setHeight(height);
		(*iter)->updateLayout();

		nNextY += height;
	}
}